//
//  XVCVistorAppointmentRecord.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/13.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCVistorAppointmentRecord.h"
#import "XTCVisitorMessage.h"
#import "XXTool.h"
#import "XVCVisitorAppointmentDetail.h"

@interface XVCVistorAppointmentRecord ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic) UILabel *noRecordLabel;

@end

@implementation XVCVistorAppointmentRecord
{
    NSMutableArray *headerTitleArray;
    NSMutableArray <XMLVisitorMessageModel*> *todayApArray;
    NSMutableArray <XMLVisitorMessageModel*> *apArray;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(UILabel*)noRecordLabel
{
    if (_noRecordLabel==nil)
    {
        _noRecordLabel = [[UILabel alloc] init];
        _noRecordLabel.text = NSLocalizedString(@"暂无预约记录", nil);
        _noRecordLabel.font = [XXTool systemFontOfSize:16.5f];
        _noRecordLabel.textAlignment = NSTextAlignmentCenter;
        _noRecordLabel.textColor = [XXTool colorWithHexString:@"838383"];
        _noRecordLabel.backgroundColor = [UIColor clearColor];
        [_tableview addSubview:_noRecordLabel];
        _noRecordLabel.hidden = YES;
    }
    return _noRecordLabel;
}

-(void)initParameter
{

}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"预约记录", nil)];
    [self tableview];
    [self noRecordLabel];
}

-(void)initData
{
    [self.view startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    _tableview.frame = CGRectMake(0, 0, frame.size.width
                                      , frame.size.height);
    _noRecordLabel.frame = _tableview.frame;
}

-(void)getData
{
    [XMLVisitorMessageModel requestVisitorMessageWithAcceptType:@"1"
                                                      listCount:0
                                                     completion:^(BOOL isSuccess, NSMutableArray<XMLVisitorMessageModel *> *datalist) {
                                                         if (isSuccess)
                                                         {
                                                             if (datalist.count==0)
                                                             {
                                                                 _noRecordLabel.hidden = NO;
                                                             }
                                                             else
                                                             {
                                                                 _noRecordLabel.hidden = YES;
                                                             }
                                                             todayApArray = [NSMutableArray array];
                                                             apArray = [NSMutableArray array];
                                                             headerTitleArray = [NSMutableArray array];
                                                             
                                                             for (XMLVisitorMessageModel* temp in datalist)
                                                             {
                                                                 if ([temp.nowflag intValue]==1)
                                                                 {
                                                                     [todayApArray addObject:temp];
                                                                 }
                                                                 else
                                                                 {
                                                                     [apArray addObject:temp];
                                                                 }
                                                             }
                                                             
                                                             if (todayApArray.count!=0)
                                                             {
                                                                 [headerTitleArray addObject:NSLocalizedString(@"今日预约", nil)];
                                                             }
                                                             else
                                                             {
                                                                 [headerTitleArray addObject:@""];
                                                             }
                                                             
                                                             if (apArray.count!=0)
                                                             {
                                                                 [headerTitleArray addObject:NSLocalizedString(@"预约列表", nil)];
                                                             }
                                                             else
                                                             {
                                                                 [headerTitleArray addObject:@""];
                                                             }
                                                             
                                                             [_tableview reloadData];
                                                         }
                                                         [_tableview.mj_header endRefreshing];
                                                         [self.view stopLoading];
                                                     }];
}

-(void)footerFresh
{
    [XMLVisitorMessageModel requestVisitorMessageWithAcceptType:@"1"
                                                      listCount:apArray.count+todayApArray.count
                                                     completion:^(BOOL isSuccess, NSMutableArray<XMLVisitorMessageModel *> *datalist) {
                                                         if (isSuccess)
                                                         {
                                                             if (datalist.count==0)
                                                             {
                                                                 
                                                             }
                                                             else
                                                             {
                                                                 for (XMLVisitorMessageModel* temp in datalist)
                                                                 {
                                                                     if ([temp.nowflag intValue]==1)
                                                                     {
                                                                         [todayApArray addObject:temp];
                                                                     }
                                                                     else
                                                                     {
                                                                         [apArray addObject:temp];
                                                                     }
                                                                 }
                                                                 [_tableview reloadData];
                                                             }
                                                             
                                                             headerTitleArray = [NSMutableArray array];
                                                             
                                                             if (todayApArray.count!=0)
                                                             {
                                                                 [headerTitleArray addObject:NSLocalizedString(@"今日预约", nil)];
                                                             }
                                                             else
                                                             {
                                                                 [headerTitleArray addObject:@""];
                                                             }
                                                             
                                                             if (apArray.count!=0)
                                                             {
                                                                 [headerTitleArray addObject:NSLocalizedString(@"预约列表", nil)];
                                                             }
                                                             else
                                                             {
                                                                 [headerTitleArray addObject:@""];
                                                             }

                                                         }
                                                         [_tableview.mj_footer endRefreshing];
                                                     }];
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTCVisitorMessage *cell = [XTCVisitorMessage cellSelectionStyleNoneWithTableView:tableView];
    if (indexPath.section==0)
    {
        [cell setCellWithData:todayApArray[indexPath.row]];
    }
    else
    {
        [cell setCellWithData:apArray[indexPath.row]];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return [XTCVisitorMessage getHeightWithData:todayApArray[indexPath.row]];
    }
    return [XTCVisitorMessage getHeightWithData:apArray[indexPath.row]];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([headerTitleArray[section] isEqualToString:@""])
    {
        return 0;
    }
    return 40;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *labelString = [self tableView:tableView titleForHeaderInSection:section];
    if ([labelString isEqualToString:@""])
    {
        return nil;
    }
    UIView *sectionContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width
                                                                      , 40)];
    sectionContent.backgroundColor = [XXTool colorWithBackGround];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, sectionContent.frame.size.width
                                                               , sectionContent.frame.size.height)];
    label.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1.0];
    label.font = [XXTool systemFontOfSize:17];
    label.text = [self tableView:tableView titleForHeaderInSection:section];
    [sectionContent addSubview:label];
    return sectionContent;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return headerTitleArray[section];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return todayApArray.count;
    }
    else if (section==1)
    {
        return apArray.count;
    }
    return 0;
}

#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMLVisitorMessageModel *temp;
    if (indexPath.section==0)
    {
        temp = todayApArray[indexPath.row];
    }
    else
    {
        temp = apArray[indexPath.row];
    }
    if(temp)
    {
        [XVCVisitorAppointmentDetail pushVistorAppointmentDetailWithSourceVC:self apmentID:temp.orderid useType:XVCVisitorAppointmentDetailIsAccepted];
    }
}

#pragma mark api
+(void)pushVisitorAppointmentRecordVCWithSourceVC:(UIViewController *)sourceVC
{
    XVCVistorAppointmentRecord *vc = [[XVCVistorAppointmentRecord alloc] init];
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
